package com.rayc.mall.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rayc.mall.common.constant.Constants;
import com.rayc.mall.common.result.PageDTO;
import com.rayc.mall.common.result.Result;
import com.rayc.mall.core.annotation.CacheRemoveByUserId;
import com.rayc.mall.order.domain.query.OrderPageQuery;
import com.rayc.mall.order.service.IOrderService;
import com.rayc.mall.order.domain.po.Order;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-26
 * @Description: 管理员订单管理相关接口
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "管理员订单管理相关接口")
@RestController
@RequestMapping("/admin/orders")
@RequiredArgsConstructor
public class AdminOrderController {
    private final IOrderService orderService;

    @ApiOperation("分页查询订单")
    @GetMapping("/page")
    public Result<PageDTO<Order>> queryOrderByPage(OrderPageQuery query) {
        log.info("分页查询订单 {}", query);
        Page<Order> pageResult = orderService.search(query);
        PageDTO<Order> result = PageDTO.of(pageResult, Order.class);
        log.info("分页查询订单结果 {}", result);
        return Result.success(result);
    }

    @ApiOperation("更新订单状态")
    @PutMapping("/status/{id}/{status}")
    // 可能是修改为发货状态，会新增物流信息，所以物流缓存也要删除
    @CacheRemoveByUserId(keys = {Constants.REDIS_KEY_ORDERS, Constants.REDIS_KEY_LOGISTICS}, removeAll = true)
    public Result<String> updateOrderStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        log.info("更新订单状态 订单id: {}, status: {}", id, status);
        orderService.updateStatus(id, status);
        log.info("更新订单状态成功 订单id: {}, status: {}", id, status);
        return Result.success();
    }
}
